/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.thereconstruction.tileset.Tiles;

/**
 *
 * @author user
 */
public class Properties2xml {
    public static void main(String[] args) {
        convert("tile/MyTileSet",0);
    }

    private static void convert(String string,int lvl) {
        try {            
            //Binding stuff
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance("org.thereconstruction.tileset");
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            
            //Virtual file stuff
            org.thereconstruction.tileset.ObjectFactory factory = new org.thereconstruction.tileset.ObjectFactory();
            Tiles tiles = factory.createTiles();
            tiles.setLevel(BigInteger.valueOf(lvl));
            List<Tiles.Tile> ts = tiles.getTile();
            
            //Loop
            PropertyResourceBundle bundle = new PropertyResourceBundle(new FileInputStream(string+"ResourceBundle.properties"));
            Iterator itr = bundle.keySet().iterator();
            while(itr.hasNext()) {
                String temp = (String) itr.next();
                Tiles.Tile tempTile = factory.createTilesTile();
                tempTile.setSymbol(temp);
                tempTile.setFile(bundle.getString(temp));
                ts.add(tempTile);
            }
            
            //Marshall it
            File xmldoc = new File(string+".xml");
            xmldoc.createNewFile();
            marshaller.marshal(tiles, xmldoc);
        } catch (javax.xml.bind.JAXBException ex) {
            // XXXTODO Handle exception
            java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
        } catch (MissingResourceException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
